<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>FreeMarker基本用法</title>
</head>
<body style="padding-left: 50px;">
    <h2>1、import指令、include指令</h2>
    <#-- 导入命名空间 -->
    <#import "common.html" as common>
    <#-- 使用命名空间中的指令 -->
    <@common.cfb num=9></@common.cfb>
    <#-- include包含指令 -->
    <p><#include "include.html"></p>
    <h2>2、基本取值</h2>
    <ul>
        <li>整数：<font color="red">${intVal}</font></li>
        <li>长整数：<font color="red">${longVal?c}</font></li>
        <li>双精度：<font color="red">${doubleVal}</font></li>
        <li>布尔：<font color="red">${booleanVal?c}、${booleanVal?string('true', 'false')}</font></li>
        <li>日期：<font color="red">${dateVal?string('yyyy-MM-dd HH:mm:ss')}</font></li>
        <li>Null：<font color="red">${nullVal!'我是默认值'}、${(dateNull?string('yyyy-MM-dd'))!'日期为空'}</font></li>
        <li>赋值&计算：<#assign calcVal=1 /><font color="red">${calcVal}、${calcVal + 100}</font></li>
        <li>对象&HTML处理：${user.name} >> ${user.desc?html}</font></li>
        <li>是否存在：${(user.name??)?c}</font></li>
    </ul>
    <h2>3、集合</h2>
    <ul>
        <li><strong>List</strong></li>
            <#if myList??>
            <#list myList as item>
            <font color="red">${item!}</font> <br/>
            </#list>
            </#if>
        <li><strong>Map</strong></li>
            <#if myMap?exists>
            <#list myMap?keys as key>
            <font color="red">${key}：${myMap[key]}</font> <br/>
            </#list>
            <#list myMap?values as val>
            <font color="red">${val}</font> <br/>
            </#list>
            </#if>
    </ul>
    <h2>4、条件判断</h2>
    <ul>
        <li><strong>if else</strong></li>
            <#assign sex=1 />
            性别：<font color="red">
            <#if sex==1>男
            <#elseif sex==2>女
            <#else>保密
            </#if>
            </font><br/>
            <#assign user="Denvie" />
            <#if user?? && user?length==6><font color="red">user不为空，且length等于6</font></#if><br/>
            <#if !(order?exists || sex!=1)><font color="red">order不存在，且sex不等于1</font></#if>
        <li><strong>switch case break default</strong></li>
            <#assign val=10 />
            <#switch val>
                <#case 10>
                <#case 20>
                    10 or 20<br/>
                    <#break>
                <#case 100>
                    100 <br/>
                    <#break>
                <#default>
                    default <br/>
            </#switch>
    </ul>
    <h2>5、字符串常用内建函数</h2>
    <ul>
        <#assign a="hello" />
        <#assign b="world" />
        <li><strong>拼接</strong></li>
            <font color="red">${a + b}</font>
        <li><strong>substring</strong></li>
            <font color="red">${(a + b)?substring(5,8)}</font>
        <li><strong>length</strong></li>
            <font color="red">${(a + b)?length}</font>
        <li><strong>upper_case</strong></li>
            <font color="red">${(a + b)?upper_case}</font>
        <li><strong>lower_case</strong></li>
            <font color="red">${(a + b)?lower_case}</font>
        <li><strong>index_of</strong></li>
            <font color="red">${(a + b)?index_of('w')}</font>
        <li><strong>last_index_of</strong></li>
            <font color="red">${(a + b)?last_index_of('o')}</font>
        <li><strong>replace</strong></li>
            <font color="red">${(a + b)?replace('o', 'O')}</font>
        <li><strong>split</strong></li>
            <#list 'a,b,c,d,e'?split(',') as item>
            <font color="red">${item}, </font>
            </#list>
        <li><strong>字符串转日期</strong></li>
            <font color="red">${'05/25/2021'?date('MM/dd/yyyy')}</font><br/>
            <font color="red">${'20:20:20'?time('HH:mm:ss')}</font><br/>
            <font color="red">${'2021-05-25 20:20:20'?datetime('yyyy-MM-dd HH:mm:ss')}</font><br/>
        <li><strong>cap_first</strong></li>
            <font color="red">${(a + b)?cap_first}</font>
        <li><strong>starts_with</strong></li>
            <font color="red">${(a + b)?starts_with('h')}</font>
        <li><strong>ends_with</strong></li>
            <font color="red">${(a + b)?ends_with('d')}</font>
        <li><strong>contains</strong></li>
            <font color="red">${(a + b)?contains('or')}</font>
        <li><strong>trim</strong></li>
            <font color="red">${(a + b)?trim}</font>
    </ul>
    <h2>6、集合常用内建函数</h2>
    <ul>
        <#assign myList=[3,2,7,5,9,7,6,8] />
        <li><strong>排序&倒序</strong></li>
            <#list myList?sort?reverse as item>
            下标（变量后加_index）：${item_index}，值：${item}，has_next（变量后加_has_next）：${item_has_next?string('true','false')}<br/>
            </#list>
        <li><strong>获取长度</strong></li>
            ${myList?size} <br/>
        <li><strong>获取指定下标元素</strong></li>
            ${myList[2]} <br/>
        <#assign groupList=[1,2,3,4,11,12,13,14,21,22,23,24] />
        <li><strong>分组size：${groupList?chunk(4)?size}</strong></li>
        最后分组：
        <#list groupList?chunk(4)?last as item>
        ${item},
        </#list>
        <li><strong>first, last, seq_contains, seq_index_of, sort_by</strong></li>
    </ul>
    <h2>7、数字常用内建函数</h2>
    <ul>
        <#assign numVal=123.45678 />
        <li>转为字符串：<strong>${numVal?c}</strong></li>
        <li>保留两位小数：<strong>${numVal?string('0.##')}</strong></li>
        <li>货币形式：<strong>${numVal?string.currency}</strong></li>
        <li>百分比形式：<strong>${numVal?string.percent}</strong></li>
        <li>四舍五入：<strong>${numVal?round}</strong></li>
        <li>向下舍位取整：<strong>${numVal?floor}</strong></li>
        <li>向上进位取整：<strong>${numVal?ceiling}</strong></li>
    </ul>
    <h2>8、其他常用内建函数</h2>
    <ul>
        <#assign val='hello' />
        <li><strong>${val?is_string?string('yes', 'no')}</strong></li>
        <li><strong>${val?is_number?string('yes', 'no')}</strong></li>
        <li><strong>${val?has_content?string('yes', 'no')}</strong></li>
    </ul>
    <h2>9、自定义函数</h2>
    <#assign myList=[3,2,7,5,9,7,6,8] />
    <ul>
        <li><strong>排序前</strong></li>
        <#list myList as item>
            ${item},
        </#list>
        <li><strong>正序</strong></li>
        <#list sort_num(myList, true) as item>
            ${item},
        </#list>
        <li><strong>倒序</strong></li>
        <#list sort_num(myList, false) as item>
            ${item},
        </#list>
    </ul>
</body>
</html>